//
//  Table.m
//  uteped_sdk
//
//  Created by mateng on 2024/2/28.
//

#import "Table.h"
#import "BaseModel.h"

@implementation Table

- (id)initWithName:(NSString*)name db:(FMDatabase*)db {
    if (self = [super init]) {
        self.name = name;
        self.db = db;
    }
    
    return self;
}

- (BOOL)updateData:(NSString *)setSql where:(NSString *)where withArgumentsInArray:(NSArray *)args {
    BOOL isSuccess = NO;
    if ([self.db open]) {
        [self.db beginTransaction];
        BOOL isRollBack = NO;
        @try {
            NSString *updateSql = [NSString stringWithFormat:@"update '%@' set %@ where %@", self.name, setSql, where];
            isSuccess = [self.db executeUpdate:updateSql withArgumentsInArray:args];
        } @catch (NSException *exception) {
            isRollBack = YES;
            [self.db rollback];
        } @finally {
            if (!isRollBack) {
                [self.db commit];
            }
        }
    }
    [self.db close];
    return isSuccess;
}

- (BOOL)deleteData:(NSString*)where withArgumentsInArray:(NSArray*)args {
    BOOL isSuccess = NO;
    if ([self.db open]) {
        [self.db beginTransaction];
        BOOL isRollBack = NO;
        @try {
            NSString *delStr = [NSString stringWithFormat:@"delete from '%@' where %@", self.name, where];
            isSuccess = [self.db executeUpdate:delStr withArgumentsInArray:args];
        } @catch (NSException *exception) {
            isRollBack = YES;
            [self.db rollback];
        } @finally {
            if (!isRollBack) {
                [self.db commit];
            }
        }
    }
    [self.db close];
    return isSuccess;
}

# pragma mark - delete recods in time range
- (BOOL)deleteData:(NSInteger)startTime endTime:(NSInteger)endTime address:(NSString*)address {
    BOOL isSuccess = NO;
    if ([self.db open]) {
        [self.db beginTransaction];
        BOOL isRollBack = NO;
        @try {
            NSString *st = [DateUtils timeStampToString:startTime fmt:FMT_TIME_CLANDER];
            NSString *et = [DateUtils timeStampToString:endTime fmt:FMT_TIME_CLANDER];
            NSString *delStr = [NSString stringWithFormat:@"delete from '%@' where calendar >= '%@' and calendar <= '%@' and address = '%@'", self.name, st, et, address];
            isSuccess = [self.db executeUpdate:delStr];
        } @catch (NSException *exception) {
            isRollBack = YES;
            [self.db rollback];
        } @finally {
            if (!isRollBack) {
                [self.db commit];
            }
        }
    }
    [self.db close];
    return isSuccess;
}

- (BOOL)saveData:(NSArray*)array {
    BOOL isSuccess = NO;
    if ([self.db open]) {
//        [self clearData]; //测试
        [self.db beginTransaction];
        BOOL isRollBack = NO;
        @try {
            for (int i = 0; i < array.count; i++) {
                BaseModel *model = array[i];
                NSString *insertStr = [NSString stringWithFormat:@"insert into '%@'(%@) values(%@)", self.name,  [model getKeyString], [model getValueString]];
                isSuccess = [self.db executeUpdate:insertStr];
            }
        } @catch (NSException *exception) {
            isRollBack = YES;
            [self.db rollback];
        } @finally {
            if (!isRollBack) {
                [self.db commit];
            }
        }
    }
    [self.db close];
}

// 仅测试使用
- (void)clearData {
    if (![self.db open]) {
        return;
    }
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM '%@'", self.name];
    BOOL res = [self.db executeUpdate: deleteSql];
    if (res) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}

@end
